package com.metadata_system.api.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * web配置
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Value("${spring.cloud.nacos.discovery.enabled}")
    Boolean isGatewayOpen;

    // 允许跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if (!isGatewayOpen){
//            System.out.printf("未开启网关");
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT","OPTION")
                    .maxAge(3600);
        }
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/webcontent/");
    }
}
